package day03;

import org.junit.Test;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

public class Demo02 {
    @Test
    public void insert() throws Exception {
        //1.获取连接
        Connection connection = JDBCUtils.getConnection();
        //2.准备sql
        String sql = "insert into user values(null,'zhangsan','123')";
        //3.获取执行平台->Statement对象
        Statement statement = connection.createStatement();
        //4.执行sql
        statement.executeUpdate(sql);
        //5.关闭资源
        JDBCUtils.close(connection, statement, null);
    }
    @Test
    public void insert1() throws Exception {
        //获取连接
        Connection connection = JDBCUtils.getConnection();
        //准备sql
        String add = "INSERT INTO `user` (username,`password`) VALUES ('王五','888');";
        String selectLastInsertId = "SELECT LAST_INSERT_ID();";

        //获取执行平台
        Statement statement = connection.createStatement();
        //执行sql
        statement.executeUpdate(add);
        ResultSet resultSet = statement.executeQuery(selectLastInsertId);
        //关闭资源
        JDBCUtils.close(connection, statement, resultSet);
    }
}
